package cool.taomu.toolkit.stock.controller

import cool.taomu.toolkit.stock.service.StockSignalService
import cool.taomu.toolkit.template.ITemplate
import cool.taomu.toolkit.utils.FileTools
import java.io.File
import java.util.Map
import javax.inject.Inject
import javax.inject.Named
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils
import org.apache.commons.vfs2.FileSystemManager
import org.glassfish.jersey.server.mvc.ErrorTemplate
import org.glassfish.jersey.server.mvc.Template
import org.slf4j.LoggerFactory
import cool.taomu.platform.common.BaseController
import cool.taomu.toolkit.stock.service.ThymeleafTemplate

@Path("stock")
class StockController extends BaseController {
    val static LOG = LoggerFactory.getLogger(StockController);

    @Inject
    @Named("taomu.http.pathSpec")
    protected String pathSpec;

    @Inject
    @Named("taomu.title")
    protected String title

    @Inject
    FileSystemManager fsManager

    @Inject
    ThymeleafTemplate templateService

    @GET
    @Path("/index")
    @ErrorTemplate(name="/error.ftl")
    @Template(name="/main.ftl")
    @Produces(MediaType.TEXT_HTML)
    def Map<String, Object> index() {
        return this.loadTemplate("股票", "res://template/stock.html", [it])
    }

    @GET
    @Path("/history")
    @ErrorTemplate(name="/error.ftl")
    @Template(name="/main.ftl")
    @Produces(MediaType.TEXT_HTML)
    def Map<String, Object> show() {
        return this.loadTemplate("股票", "res://template/stockhistory.html", [it])

    }

    @Inject
    StockSignalService stockSignalService

    @GET
    @Path("/signal")
    @ErrorTemplate(name="/error.ftl")
    @Template(name="/main.ftl")
    @Produces(MediaType.TEXT_HTML)
    def Map<String, Object> signal() {
        return this.loadTemplate("股票", "res://template/stocksignal.html", [ a |
            var data = stockSignalService.signalGroup
            var tfile = File.createTempFile("html", ".html")
            FileUtils.writeStringToFile(tfile, a, "UTF-8")
            return templateService.template.execute(tfile.absolutePath, #{"signalGroups" -> data}).toString();
        ])
    }

    @GET
    @Path("/signal2")
    @ErrorTemplate(name="/error.ftl")
    @Template(name="/main.ftl")
    @Produces(MediaType.TEXT_HTML)
    def Map<String, Object> signal2() {
         return this.loadTemplate("股票", "res://template/stocksignal.html", [ a |
             var data = stockSignalService.llm2
            var tfile = File.createTempFile("html", ".html")
            FileUtils.writeStringToFile(tfile, a, "UTF-8")
            return templateService.template.execute(tfile.absolutePath, #{"signalGroups" -> data}).toString();
        ])
    }

    @GET
    @Path("/signal3")
    @ErrorTemplate(name="/error.ftl")
    @Template(name="/main.ftl")
    @Produces(MediaType.TEXT_HTML)
    def Map<String, Object> signal3() {
        try(var fobject = fsManager.resolveFile("res://template/stocksignal2.html")) {
            var a = IOUtils.toString(fobject.content.inputStream, "UTF-8");
            a = FileTools.extractBetweenComments(a, "<!-- PAGE CONTENT BEGINS -->", "<!-- PAGE CONTENT ENDS -->")
            var data = stockSignalService.SignalDetailSummary("")
            var tfile = File.createTempFile("html", ".html")
            FileUtils.writeStringToFile(tfile, a, "UTF-8")
            a = templateService.template.execute(tfile.absolutePath, #{"signalGroups" -> data}).toString();
            return #{"pathSpec" -> pathSpec, "title" -> title, "menus" -> menuService.menuList, "navbar" -> "nav",
                "content" -> a, "userid" -> "0", "name" -> "股票"}
        }
    }

    @GET
    @Path("/signal3/{stype}")
    @ErrorTemplate(name="/error.ftl")
    @Template(name="/main.ftl")
    @Produces(MediaType.TEXT_HTML)
    def Map<String, Object> signal3(@PathParam("stype") String signlType) {
        try(var fobject = fsManager.resolveFile("res://template/stocksignal2.html")) {
            var a = IOUtils.toString(fobject.content.inputStream, "UTF-8");
            a = FileTools.extractBetweenComments(a, "<!-- PAGE CONTENT BEGINS -->", "<!-- PAGE CONTENT ENDS -->")
            var data = stockSignalService.SignalDetailSummary(signlType)
            var tfile = File.createTempFile("html", ".html")
            FileUtils.writeStringToFile(tfile, a, "UTF-8")
            a = templateService.template.execute(tfile.absolutePath, #{"signalGroups" -> data}).toString();
            return #{"pathSpec" -> pathSpec, "title" -> title, "menus" -> menuService.menuList, "navbar" -> "nav",
                "content" -> a, "userid" -> "0", "name" -> "股票"}
        }
    }
    
    @GET
    @Path("/signal3/{stype}/{row}/{page}")
    @ErrorTemplate(name="/error.ftl")
    @Template(name="/main.ftl")
    @Produces(MediaType.TEXT_HTML)
    def Map<String, Object> signal3Page(@PathParam("stype") String signlType
        ,@PathParam("row") Integer row,@PathParam("page") Integer page
    ) {
        try(var fobject = fsManager.resolveFile("res://template/stocksignal2.html")) {
            var a = IOUtils.toString(fobject.content.inputStream, "UTF-8");
            a = FileTools.extractBetweenComments(a, "<!-- PAGE CONTENT BEGINS -->", "<!-- PAGE CONTENT ENDS -->")
            var offset = page;
            var data = stockSignalService.SignalDetailSummaryPage(signlType,row,offset == 0 ? 1 : offset)
            var tfile = File.createTempFile("html", ".html")
            FileUtils.writeStringToFile(tfile, a, "UTF-8")
            a = templateService.template.execute(tfile.absolutePath, #{"signalGroups" -> data,"row"->row,"page"->page}).toString();
            return #{"pathSpec" -> pathSpec, "title" -> title, "menus" -> menuService.menuList, "navbar" -> "nav",
                "content" -> a, "userid" -> "0", "name" -> "股票"}
        }
    }
}
